package com.superman.supermarket.client.view.sku;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.StringUtils;
import com.superman.mvpframe.client.widget.lazy.LazyImage;
import com.superman.supermarket.client.composite.common.banner.ImageBanner;
import com.superman.supermarket.client.composite.sku.bottom.SkuDetailBottomModule;
import com.superman.supermarket.client.composite.sku.comment.SKUCommentListModule;
import com.superman.supermarket.client.utils.ActivityJumpUrl;

/**
 * 商品详情UI实现类
 * 
 * @author superman
 * @version 2018年7月23日下午9:40:25
 */
public class SKUViewImpl extends Composite implements SKUView {

	interface SKUViewImplUiBinder extends UiBinder<Widget, SKUViewImpl> {
	}

	private static SKUViewImplUiBinder uiBinder = GWT.create(SKUViewImplUiBinder.class);

	@UiField
	SkuDetailBottomModule bottom;
	@UiField
	SKUCommentListModule commentlistmodule;
	@UiField
	FlowPanel couponpanel;
	@UiField
	ImageBanner imagebanner;
	private SKUViewPresenter presenter;
	@UiField
	HTML salepricehtml, listpricehtml, skunamehtml, skudeschtml, couponnameshtml, storenamehtml, deliveryhtml, postfeehtml, picdetailhtml,
			propnameshtml;

	private String storeId;

	@UiField
	LazyImage storeimg;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年7月23日 下午9:40:40
	 */
	public SKUViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		couponpanel.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// 领券
				presenter.pushToStoreCouponListPage();
			}
		}, ClickEvent.getType());

		commentlistmodule.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// 评价列表
				presenter.pushToCommentListPage();
			}
		}, ClickEvent.getType());
	}

	@Override
	public void changeFavStatus(boolean isFav) {
		bottom.setFav(isFav);
	}

	@Override
	public void init() {
		picdetailhtml.getElement().setId("pic-detail-wrapper");
	}

	@Override
	public void removeCommentListModule() {
		commentlistmodule.removeFromParent();
	}

	@Override
	public void removePicDetailModule() {
		picdetailhtml.removeFromParent();
	}

	@Override
	public void setCommentCountAndCommentList(int count, List<Map<String, Object>> list) {
		commentlistmodule.setCount(count);
		// commentlistmodule.setListData(list);
	}

	@Override
	public void setCouponInfo(Map<String, Object> couponItem) {
		if (couponItem == null)
			couponpanel.removeFromParent();
		else {
			String couponNameStr = String.valueOf(couponItem.get("NAME"));
			couponnameshtml.setText(couponNameStr);
		}
	}

	@Override
	public void setDeliveryInfo(String dispathAddr, String postfee) {
		deliveryhtml.setText(dispathAddr);
		postfeehtml.setText(postfee);
	}

	@Override
	public void setDetailPropNames(String propNames) {
		propnameshtml.setText(propNames);
	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		this.presenter = (SKUViewPresenter) presenter;
	}

	@Override
	public void setPicDetailHTML(String html) {
		picdetailhtml.setHTML(html);
	}

	@Override
	public void setSkuImages(List<String> list) {
		imagebanner.clear();
		for (int i = 0; i < list.size(); i++) {
			Image image = new Image(list.get(i));
			imagebanner.addItem(image);
		}
		imagebanner.init(false);
	}

	@Override
	public void setSkuNameAndOtherInfo(String skuName, String desc, double salePrice, double listPrice, boolean isFav) {
		salepricehtml.setHTML("¥&nbsp;" + salePrice);
		listpricehtml.setHTML("¥&nbsp;" + listPrice);
		skunamehtml.setText(skuName);
		if (!StringUtils.isEmpty(desc))
			skudeschtml.setText(desc);
		bottom.setFav(isFav);
	}

	@Override
	public void setStoreInfo(String storeId, String storeLogo, String storeName) {
		this.storeId = storeId;
		storenamehtml.setText(storeName);
		storeimg.setUrl(storeLogo);

	}

	@UiHandler("tostorehtml")
	void toStorePage(ClickEvent event) {
		ActivityJumpUrl.pushToStorePage(storeId);
	}

}
